/**
 * 
 */



package com.UMLife.me.action;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;





/**
 * Class Information:
 * 
 * @Author yangguang
 * @Date 2009-11-8
 * @Description <p>
 *              这个类用来存储用户登录时的设置,内容如下：
 *              <p>
 *              1.用户名 2.密码 3.是否自动登录 4.是否存储密码
 *              <p>
 *              2.当用户名或密码为空时，不进行存储。
 *              <p>
 *              3.当存储密码为true时，密码设置为所给参数内容，否则自动设置为""。
 * @MustDos 使用方法：
 *          <p>
 *          1.使用者不需要使用构造函数，只需使用静态函数getLoginData，获取LoginData
 *          <p>
 *          对象即可对LoginData进行存取操作。这样设计的目的是 ：
 *          <p>
 *          <1>采用单例设计模式，无论何时，不允许构造多个LoginData，以便节省空间。
 *          <p>
 *          <2>每次使用的时候只需使用getLoginData就可以省去rms的解码问题，方便使用这
 *          <p>
 *          个类的用户操作。
 *          <p>
 *          2.使用set方法之后一定要进行保存.
 */
public class LoginData
{
	
	private LoginData()
	{
		
	}
	
	public static LoginData getLoginData()
	{
		if (LoginData.loginData == null)
		{
			LoginData.loginData = new LoginData();
			LoginData.loginData.decode();
		}
		return LoginData.loginData;
	}
	
	/**
	 * @return the password
	 */
	public String getPassword()
	{
		return this.password;
	}
	
	/**
	 * @return the rememberPassword
	 */
	public boolean getRememberPassword()
	{
		return (this.rememberPassword.endsWith("true") ? true : false);
	}
	
	/**
	 * @return the selfLogin
	 */
	public boolean getSelfLogin()
	{
		
		return (this.selfLogin.endsWith("true") ? true : false);
	}
	
	/**
	 * @return the userName
	 */
	public String getUserName()
	{
		
		return this.userName;
	}
	
	public void save()
	{
		if ((this.userName.length() == 0) || (this.password.length() == 0))
		{
			return;
		}
		
		byte[] userNameBytes = this.userName.getBytes();
		byte[] passwordBytes = this.password.getBytes();
		byte[] selfLoginBytes = this.selfLogin.getBytes();
		byte[] rememberPasswordBytes = this.rememberPassword.getBytes();
		
		try
		// 这个try块在已经建立该用户存储信息的时候使用
		{
			this.storage = RecordStore.openRecordStore(
					LoginData.recordStoreName, false);
			this.storage.setRecord(1, userNameBytes, 0, userNameBytes.length);
			this.storage.setRecord(2, passwordBytes, 0, passwordBytes.length);
			this.storage.setRecord(3, selfLoginBytes, 0, selfLoginBytes.length);
			this.storage.setRecord(4, rememberPasswordBytes, 0,
					rememberPasswordBytes.length);
			
		} catch (RecordStoreFullException e)
		{
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e)
		{
			try
			// 这个try块在新建立用户信息的时候使用
			{
				this.storage = RecordStore.openRecordStore(
						LoginData.recordStoreName, true);
				this.storage.addRecord(userNameBytes, 0, userNameBytes.length);
				this.storage.addRecord(passwordBytes, 0, passwordBytes.length);
				this.storage
						.addRecord(selfLoginBytes, 0, selfLoginBytes.length);
				this.storage.addRecord(rememberPasswordBytes, 0,
						rememberPasswordBytes.length);
			} catch (RecordStoreFullException e1)
			{
				e1.printStackTrace();
			} catch (RecordStoreNotFoundException e1)
			{
				e1.printStackTrace();
			} catch (RecordStoreException e1)
			{
				e1.printStackTrace();
			}
			
		} catch (RecordStoreException e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				this.storage.closeRecordStore();
			} catch (RecordStoreNotOpenException e)
			{
				e.printStackTrace();
			} catch (RecordStoreException e)
			{
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password)
	{
		if (password != null)
		{
			this.password = password;
			
		}
		
	}
	
	/**
	 * @param rememberPassword
	 *            the rememberPassword to set
	 */
	public void setRememberPassword(boolean rememberPassword)
	{
		
		this.rememberPassword = (rememberPassword == true ? "true" : "false");
		if (!rememberPassword)
		{
			this.password = "";
		}
	}
	
	/**
	 * @param selfLogin
	 *            the selfLogin to set
	 */
	public void setSelfLogin(boolean selfLogin)
	{
		this.selfLogin = (selfLogin == true ? "true" : "false");
		
	}
	
	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserName(String userName)
	{
		if (userName != null)
		{
			this.userName = userName;
		}
		
	}
	
	/******************** 实现细节部分，使用者不用关心 *************************/
	
	private void decode()
	{
		
		try
		// 在已经建立存储的情况下，直接取就可以了。
		{
			this.storage = RecordStore.openRecordStore(
					LoginData.recordStoreName, false);
			this.userName = new String(this.storage.getRecord(1));
			this.password = new String(this.storage.getRecord(2));
			this.selfLogin = new String(this.storage.getRecord(3));
			this.rememberPassword = new String(this.storage.getRecord(4));
		} catch (RecordStoreFullException e)
		{
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e)
		{
			// 在没有找到存储的情况下,使用各个参数的初始化数据。
			
		} catch (RecordStoreException e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				this.storage.closeRecordStore();
			} catch (RecordStoreNotOpenException e)
			{
				e.printStackTrace();
			} catch (RecordStoreException e)
			{
				e.printStackTrace();
			}
		}
		
	}
	
	private static LoginData	loginData;
	
	private static final String	recordStoreName		= "LoginData";
	
	private String				password			= "";
	
	private String				rememberPassword	= "false";
	
	private String				selfLogin			= "false";
	
	private RecordStore			storage;
	
	private String				userName			= "";
	
}
